package cn.com.winning.ecare.activity;

//暂时不用

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**健康知识-停用
 * @author Administrator
 *
 */
public class HealthKnowledge extends Activity {

	private HealthKnowledge oThis = this;
	private CustomProgressDialog proDialog;// 加载框
	private ListView lv = null;
	private Button tv_lb = null;
	private ImageButton btnok = null;
	private EditText edt_gjz = null;
	private Map<String, Object> map_health;
	List<Map<String, Object>> list_health = new ArrayList<Map<String, Object>>();
	private String[] arrId, arrMc;

	private String strId, strGJZ;
	InputMethodManager manager;

	private int preId = -1, preId1 = -1;
	private MyAdapter myAdapter;
	private MyAdapter1 myAdapter1;
	MyApplication myApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_main);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		setTitle("健康知识");
		// 初始化view

		findView();

		// 初始化界面数据
		GetData();

		// 点击事件
		setOnClick();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null
					&& getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);
	}

	private void iniSpinner() {

		// ArrayAdapter<String> adapter = new ArrayAdapter<String>(
		// HealthKnowledge.this, android.R.layout.simple_spinner_item,
		// arrMc);
		//
		// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//
		// // 将adapter 添加到spinner中
		// sp.setAdapter(adapter);
		//
		// // 设置默认值
		// sp.setVisibility(View.VISIBLE);

	}

	private void findView() {
		lv = (ListView) this.findViewById(R.id.list);
		tv_lb = (Button) this.findViewById(R.id.tv_lb);
		btnok = (ImageButton) this.findViewById(R.id.btncx);
		edt_gjz = (EditText) this.findViewById(R.id.edt_gjz);
		// edt_gjz.setText("毛巾");
	}

	private void GetData() {
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData() {
		try {
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			// ls.add(new BasicNameValuePair("userid", myApp.getAccount()));
			ls.add(new BasicNameValuePair("id", "0"));
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.HEALTH, ls);

			if (json != null) {

				// JSONArray jsonArr = new JSONArray(json.getString("tl"));
				JSONArray jsonArr = json.getJSONArray("orglist");
				JSONObject js = null;
				arrId = new String[jsonArr.length()];
				arrMc = new String[jsonArr.length()];

				for (int i = 0; i < jsonArr.length(); i++) {
					map_health = new HashMap<String, Object>();
					js = jsonArr.getJSONObject(i);
					map_health.put("id", js.getString("id"));
					map_health.put("lbmc", js.getString("lbmc"));
					list_health.add(map_health);
					arrId[i] = js.getString("id");
					arrMc[i] = js.getString("lbmc");
				}
			}
			msg.what = 0;
			myHandler.sendEmptyMessage(msg.what);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				proDialog.dismiss();
				setData();
				// 初始化spinner
				// iniSpinner();
				break;
			default:
				break;
			}
		};
	};

	private void setData() {
		myAdapter = new MyAdapter(this);
		myAdapter.setList(list_health);
		lv.setAdapter(myAdapter);
	}

	public class ViewHolder {
		private TextView tv_id;
		private TextView tv_mc;
	}

	class MyAdapter extends BaseAdapter {

		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public MyAdapter(Context context) {
			mLayoutInflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList() {
			return list;
		}

		public void setList(List<Map<String, Object>> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater
						.inflate(R.layout.health_sub, null);
				holder = new ViewHolder();
				holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				holder.tv_mc = (TextView) convertView.findViewById(R.id.tv_mc);
				convertView.setTag(holder);

			} else
				holder = (ViewHolder) convertView.getTag();
			holder.tv_id.setText(list.get(position).get("id").toString());
			holder.tv_mc.setText(list.get(position).get("lbmc").toString());
			if (preId == position)
				convertView.setBackgroundColor(getResources().getColor(
						R.color.clickRemeber));
			else
				convertView.setBackgroundColor(getResources().getColor(
						R.color.backgroundMYD));
			return convertView;
		}
	}

	class MyAdapter1 extends BaseAdapter {

		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public MyAdapter1(Context context) {
			mLayoutInflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList() {
			return list;
		}

		public void setList(List<Map<String, Object>> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mLayoutInflater.inflate(R.layout.health_lb_item,
						null);
				holder = new ViewHolder();
				holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				holder.tv_mc = (TextView) convertView.findViewById(R.id.tv_mc);
				convertView.setTag(holder);

			} else
				holder = (ViewHolder) convertView.getTag();
			holder.tv_id.setText(list.get(position).get("id").toString());
			holder.tv_mc.setText(list.get(position).get("lbmc").toString());
			if (preId1 == position)
				convertView.setBackgroundColor(getResources().getColor(
						R.color.clickRemeber));
			else
				convertView.setBackgroundColor(getResources().getColor(
						R.color.backgroundMYD));
			return convertView;
		}
	}

	private void setOnClick() {
		// 点击明细
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				TextView tv_id = (TextView) arg1.findViewById(R.id.tv_id);
				myAdapter.notifyDataSetChanged();
				strId = tv_id.getText().toString();
				preId = arg2;
				Intent intent = new Intent(HealthKnowledge.this,
						HealthKnowledgeMX.class);
				intent.putExtra("strId", strId);
				intent.putExtra("strGJZ", "");
				startActivity(intent);

			}
		});

		// 添加事件Spinner事件监听
		// sp.setOnItemSelectedListener(new OnItemSelectedListener() {
		//
		// @Override
		// public void onItemSelected(AdapterView<?> arg0, View arg1,
		// int arg2, long arg3) {
		// // TODO Auto-generated method stub
		// // view.setText("你的血型是：" + m[arg2]);
		// // Toast.makeText(this, arrId[arg2], Toast.LENGTH_LONG).show();
		// Log.i("aaa", arrId[arg2]);
		// strId = "";
		// strId = arrId[arg2];
		// }

		// @Override
		// public void onNothingSelected(AdapterView<?> arg0) {
		// // TODO Auto-generated method stub
		//
		// }
		// });

		// 添加查询事件
		btnok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (strId == null) {
					Toast.makeText(HealthKnowledge.this, "请先选择类别",
							Toast.LENGTH_SHORT).show();
					return;
				}
				strGJZ = edt_gjz.getText().toString().trim();
				Intent intent = new Intent(HealthKnowledge.this,
						HealthKnowledgeMX.class);
				intent.putExtra("strId", strId);
				intent.putExtra("strGJZ", strGJZ);

				startActivity(intent);

			}
		});

		// 弹出选择类别
		tv_lb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				AlertDialog.Builder builder = new AlertDialog.Builder(
						HealthKnowledge.this);

				builder.setTitle("健康知识类别");

				// final Dialog dialog = new Dialog(CostSelect.this,
				// R.style.MyDialog);
				//
				LayoutInflater factory = LayoutInflater
						.from(HealthKnowledge.this);
				View dialogView = factory.inflate(R.layout.health_lb, null);
				builder.setView(dialogView);
				ListView listSfmx = (ListView) dialogView
						.findViewById(R.id.list_lb);
				// listSfmx.setAdapter(new SimpleAdapter(HealthKnowledge.this,
				// list_health, R.layout.health_lb_item, new String[] {
				// "lbmc", "id" }, new int[] { R.id.tv_mc,
				// R.id.tv_id }));

				myAdapter1 = new MyAdapter1(HealthKnowledge.this);
				myAdapter1.setList(list_health);
				listSfmx.setAdapter(myAdapter1);
				// builder.setNegativeButton("确定", null);

				final Dialog dialog = builder.create();
				dialog.show();

				listSfmx.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						arg1.setBackgroundColor(getResources().getColor(
								R.color.clickRemeber));
						myAdapter1.notifyDataSetChanged();
						TextView tv_id = (TextView) arg1
								.findViewById(R.id.tv_id);
						TextView tv_mc = (TextView) arg1
								.findViewById(R.id.tv_mc);
						strId = tv_id.getText().toString();
						tv_lb.setText(tv_mc.getText().toString().trim());
						preId1 = arg2;
						dialog.dismiss();

					}
				});

			}
		});

	}
}
